<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PropertyPortlet.php 425 2011-01-05 10:30:30Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalConfigRecord');
Prado::using('System.Web.UI.WebControls.TDropDownList');

/*
 * Property of pportlet
 */
class PropertyPortlet extends PortalSystemPage
{
	private $_pagename		= '';
	private $_controlname	= '';
	private $_portletname	= '';
	private $_recid			= 0;

	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->User)
		{
			$this->reportError(1,Prado::localize("You need to be logged in to use this page"));
		}

		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			$this->reportError(1,Prado::localize("You need to be logged in to use this page"));
		}

		if(($pid=$this->Request['pid'])!==null)
		{
			$this->pid->Value	= $pid;
			$this->PageName		= $pid;
		}

		if(($cid=$this->Request['cid'])!==null)
		{
			$this->cid->Value	= $cid;
			$this->_controlname	= $cid;
		}

		if(($poid=$this->Request['poid'])!==null)
		{
			$this->rid->Value	= $poid;
			$this->_recid		= $poid;
		}

		if(($pname = $this->Request['pname'])!==null)
		{
			$this->PortletName	= $pname;
		}

		// Get Portlet properties
		$pCommonClassName	= $this->PortletName . 'Common';
		if(!class_exists($pCommonClassName,false))
		{
			$incFile = 'Application.Portlets.' . $this->PortletName . '.Common.' . $pCommonClassName;
			Prado::using($incFile);
		}

		if(method_exists($pCommonClassName,'getCustomProperties'))
		{
			$pProperties = call_user_func($pCommonClassName . '::getCustomProperties');
			if(is_array($pProperties) && count($pProperties) > 0)
			{
				$this->tblCustomProperties->Visible	= true;
				$this->tblCustomProperties->Enabled	= true;

				// get values...
				$config = PortalConfigRecord::finder()->findByname($this->ConfigName);

				if($config)
				{
					$pVal = Prado::unserialize($config->value);
				}
				else
				{
					$pVal	= array();
				}

				foreach($pProperties as $property)
				{
					$row	= new TTableRow;
					$cell	= new TTableCell;
					$cell->Text	= $property['title'];
					$row->Cells->add($cell);

					$cell	= new TTableCell;
					$value = null;

					$ctrl	= $this->buildPropertyControl($property,$pVal);

					if($ctrl !== null)
					{
						$cell->Controls[] = $ctrl;
					}
					else
					{
						$cell->Text = "&nbsp;";
					}

					$row->Cells->add($cell);
					$this->tblCustomProperties->Rows->add($row);
				}
			}
			else
			{
				$this->tblCustomProperties->Visible	= false;
				$this->tblCustomProperties->Enabled	= false;
			}
		}
	}

	public function onLoad($param)
	{
		$this->Title = $this->Application->Parameters['SiteTitle'] . " - " . Prado::localize("Element properties");
		if(!$this->IsPostBack)
		{
			$row = $this->DataAccess->createCommand("select * from tblpagecontent where id=" . $this->_recid)->queryRow();

			$this->LoginRequired->Checked		= TPropertyValue::ensureBoolean($row['needlogin']);
			$this->ShowWhenLoggedin->Checked	= TPropertyValue::ensureBoolean($row['showafterlogin']);
			$this->Active->Checked				= TPropertyValue::ensureBoolean($row['active']);

			// Get Portlet properties
			$pCommonClassName	= $this->PortletName . 'Common';
			if(!class_exists($pCommonClassName,false))
			{
				$incFile = 'Application.Portlets.' . $this->PortletName . '.Common.' . $pCommonClassName;
				Prado::using($incFile);
			}

			if(!method_exists($pCommonClassName,'getCustomProperties'))
			{
				$this->tblCustomProperties->Visible	= false;
				$this->tblCustomProperties->Enabled	= false;
			}
		}
	}

	private function buildPropertyControl($prop,$savedValues)
	{
		$type	= $prop['type'];
		$id		= $prop['name'];
		$value	= (isset($savedValues[$id]) ? $savedValues[$id] : null);
		$ctrl	= null;

		switch($type)
		{
			case 'colorpick':
				$ctrl = new TColorPicker();
				$ctrl->Id = $id;
				$ctrl->Mode	='Basic';

				if($value !== null)
				{
					$ctrl->Text	= $value;
				}
			break;

			case 'text':
			case 'number':
				$ctrl	= new TTextBox();
				$ctrl->Id	= $id;
				if($value !== null)
				{
					$ctrl->Text	= $value;
				}
				break;

			case 'droplist':
				$ctrl = new TDropDownList();

				$ctrl->Id				= $id;
				$dlvalues				= $prop['values'];
				$ctrl->DataSource		= $dlvalues;
				$ctrl->DataTextField	= "name";
				$ctrl->DataValueField	= "value";
				$ctrl->PromptText		= Prado::localize("Select");
				$ctrl->PromptValue		= "";
				$ctrl->dataBind();

				if($value)
				{
					try
					{
						$ctrl->SelectedValue = $value;
					}
					catch(Exception $e) {}
				}
				break;

			case 'tablepick':
				$selecttable	= $prop['values'][0];
				$selectfields	= $prop['values'][1];
				$selectvalue	= $prop['values'][2];
				$where			= $prop['values'][3];

				$dlvalues		= $this->DataAccess->createCommand(ltrim(rtrim("select $selectfields from $selecttable $where")))->query();

				$ctrl = new TDropDownList();
				$ctrl->Id				= $id;
				$ctrl->DataSource		= $dlvalues;
				$ctrl->DataTextField	= $selectfields;
				$ctrl->DataValueField	= $selectvalue;
				$ctrl->PromptText		= Prado::localize("Select");
				$ctrl->PromptValue		= "";
				$ctrl->dataBind();
				break;

		}

		return $ctrl;
	}

	public function getPortletName()
	{
		return $this->_portletname;
	}

	public function setPortletName($value)
	{
		$this->_portletname = $value;
	}

	public function saveButtonClicked($sender,$param)
	{
		$rid			= $this->rid->Value;
		$needlogin		= TPropertyValue::ensureInteger($this->LoginRequired->Checked);
		$showafterlogin	= TPropertyValue::ensureInteger($this->ShowWhenLoggedin->Checked);
		$active			= TPropertyValue::ensureInteger($this->Active->Checked);
		$sql			= "update tblpagecontent set needlogin='$needlogin',showafterlogin='$showafterlogin',active='$active' where id=$rid";
		$this->DataAccess->createCommand($sql)->execute();

		// Get Portlet properties
		$pCommonClassName	= $this->PortletName . 'Common';
		if(!class_exists($pCommonClassName,false))
		{
			$incFile = 'Application.Portlets.' . $this->PortletName . '.Common.' . $pCommonClassName;
			Prado::using($incFile);
		}

		if(method_exists($pCommonClassName,'getCustomProperties'))
		{
			$pProperties = call_user_func($pCommonClassName . '::getCustomProperties');
			if(is_array($pProperties) && count($pProperties) > 0)
			{
				$pVal	= array();
				foreach($pProperties as $property)
				{
					$ctrl	= $this->tblCustomProperties->findControl($property['name']);
					switch($property['type'])
					{
						case 'colorpick':
						case 'text':
						case 'number':
							$pVal[$property['name']]	= $ctrl->Text;
							break;

						case 'droplist':
							$pVal[$property['name']]	= $ctrl->SelectedValue;
							break;
					}
				}

				// Save
				$config = PortalConfigRecord::finder()->findByname($this->ConfigName);
				if(!$config)
				{
					$config = new PortalConfigRecord();
					$config->name	= $this->ConfigName;
				}

				$config->value	= Prado::serialize($pVal);
				$config->save();
			}
		}

		$this->gotoPage($this->pid->Value,array("PageMode" => $this->Page->EditMode));
	}

	public function cancelButtonClicked($sender,$param)
	{
		$rid			= $this->rid->Value;
		$this->gotoPage($this->pid->Value,array("PageMode" => $this->Page->EditMode));
	}

	public function getPageName()
	{
		return $this->_pagename;
	}

	public function setPageName($value)
	{
		$this->_pagename = $value;
	}

	public function ControlName()
	{
		return $this->_controlname;
	}

	public function ElementName()
	{
		return $this->_portletname;
	}

	public function getConfigName()
	{
		return 'prop_' . $this->PortletName . '_' . $this->PageName . '_' . $this->_recid;
	}
}
?>